package com.ds.common;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
public class R<T> {
    public final static int SUCCESS = 0;
    public final static String SUCCESS_MSG = "操作成功";
    public final static int FAILED = -1;
    public final static String FAILED_MSG = "操作失败";

    /**
     * 响应code
     */
    private int code;

    /**
     * 响应信息
     */
    private String msg;

    /**
     * 响应数据
     */
    private T data;

    public static R<String> ok() {
        return restResult(null, SUCCESS, SUCCESS_MSG);
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, SUCCESS, SUCCESS_MSG);
    }

    public static <T> R<List<T>> list(List<T> list) {
        return restResult(list, SUCCESS, SUCCESS_MSG);
    }

    public static <T> R<T> failed(String msg, T data) {
        return restResult(data, FAILED, msg);
    }

    public static <T> R<T> failed(int code, String msg, T data) {
        return restResult(data, code, msg);
    }

    public static <T> R<T> failed(int code, String msg) {
        return restResult(null, code, msg);
    }

    public static <T> R<T> failed(String msg) {
        return restResult(null, FAILED, msg);
    }

    public static R<String> failed() {
        return restResult(null, FAILED, FAILED_MSG);
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, FAILED, FAILED_MSG);
    }

    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> r = new R<>();
        r.code = code;
        r.data = data;
        r.msg = msg;
        return r;
    }
}
